package com.bosi.dalian.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;


@Configuration
public class DataSourceConfig {

    public final static String BUSI_DATASOURCE_KEY = "busi";
    public final static String DL_DATASOURCE_KEY = "dldatasource";

    @Value("${spring.datasource.type}")
    private Class<? extends DataSource> dataSourceType;


    @Primary
    @Bean(name = BUSI_DATASOURCE_KEY)
    @Qualifier(BUSI_DATASOURCE_KEY)
    @ConfigurationProperties(prefix = "spring.datasource.busi")
    public DataSource busi() {
        System.out.println("create master "+BUSI_DATASOURCE_KEY+"...");
        return DataSourceBuilder.create().type(dataSourceType).build();
    }

    @Bean(name = DL_DATASOURCE_KEY)
    @Qualifier(DL_DATASOURCE_KEY)
    @ConfigurationProperties(prefix = "spring.datasource.dldatasource")
    public DataSource dldatasource() {
        System.out.println("create master "+DL_DATASOURCE_KEY+"...");
        return DataSourceBuilder.create().type(dataSourceType).build();
    }


}
